
#include "TM1637.h"
#include "Time_Show.h"
#include "stdio.h"
#include "usart.h"
#include "delay.h"
#include "main.h"

typedef struct {
    uint8_t hour;
    uint8_t minute;
    uint8_t second;
    
}sTIME;

sTIME s_time;
sTIME s_time_bak;

void Time_Init(uint16_t hour, uint8_t minute)
{
    if(hour>=24){
        debug_err("Error: hour=%d\r\n", hour);
        hour = 0;
        minute = 0;
    }
    if(minute>=60){
        debug_err("Error: minute=%d\r\n", minute);
        hour = 0;
        minute = 0;
    }
    
    s_time.hour = hour;
    s_time.minute = minute;
    Time_Show();
}


void Time_Add_a_second(void)
{
    uint8_t bit_1;
    
    debug_inf("time:%02d:%02d:%02d\r\n", s_time.hour, s_time.minute, s_time.second);
    s_time.second++;
    
    /*冒号闪烁*/
    bit_1 = s_time.hour%10;
    TM1637_SmgDisplay_44(1, bit_1, s_time.second%2);
    //myprintf("\r\n");
    /*时间分钟级更新*/
    if(s_time.second>=60){
        s_time.second=0;
        s_time.minute++;
        if(s_time.minute>=60)
        {
            s_time.minute=0;
            s_time.hour++;
            if(s_time.hour>=24)
            {
                s_time.hour=0;
            }
        }
        /*一分钟就该更新显示*/
        Time_Show();
    }
    
}


void Time_Show(void)
{
    uint8_t time_list[4];
    
    time_list[0] = s_time.hour/10 ;
    time_list[1] = s_time.hour%10 ;
    time_list[2] = s_time.minute/10 ;
    time_list[3] = s_time.minute%10 ;
    
    TM1637_SmgDisplay_40(time_list);
}


void Time_bak(void)
{
    s_time_bak = s_time;
}

/*
    位闪烁，闪烁位为选中位
bit:0,1,2,3
*/
int Time_Bit_flash(uint8_t bit)
{
    if(bit>3){
        debug_err("error:bit[%d]>3\r\n", bit);
        return -1;
    }
    if(modify_mode != mode){
        debug_err("error:mode %d\r\n", mode);
        return -1;
    }
    TM1637_SmgDisplay_44(bit, 11, 0);

    return 0;
}

/*
    返回增加后的值
*/
void top_add(uint8_t source, uint8_t* dest, uint8_t max){
    source++;
    if(source>max){
        *dest = 0;
    }else{
        *dest = source;
    }
}


int Time_Bit_add(uint8_t bit)
{
    if(bit>3){
        debug_err("error:bit[%d]>3\r\n", bit);
        return -1;
    }
    if(add_mode != mode){
        debug_err("error:mode %d\r\n", mode);
        return -2;
    }
        
    uint8_t hour_add=0;
    uint8_t minute_add=0;
    uint8_t time_list[4];
    
    time_list[0] = s_time.hour/10 ;
    time_list[1] = s_time.hour%10 ;
    time_list[2] = s_time.minute/10 ;
    time_list[3] = s_time.minute%10 ;
    
    switch(bit){
    case 0:
        top_add(time_list[0], &hour_add, 2);
        myprintf(" %d", hour_add);
        s_time.hour = hour_add * 10 + time_list[1];
        if(s_time.hour > 23){
            s_time.hour = 23;
        }
        break;
    case 1:
        if(time_list[0] != 2){
            top_add(time_list[1], &hour_add, 9);
        }else{
            top_add(time_list[1], &hour_add, 3);
        }
        myprintf(" %d", hour_add);
        s_time.hour =  time_list[0] * 10 + hour_add;
        break;  
    case 2:
        top_add(time_list[2], &minute_add, 5);
        myprintf("%d", minute_add);
        s_time.minute = minute_add*10 + time_list[3];
        break;
    case 3:
        top_add(time_list[3], &minute_add, 9);
         myprintf(" %d", minute_add);
        s_time.minute = time_list[2] * 10 + minute_add;
        break;
    default:
        break;
    }
    printf("\r\n");
    if(s_time.hour >= 24){
        debug_err("error:Configuration failed bit[%d], hour[%d] to [%d] \r\n", bit, s_time.hour, s_time.hour + hour_add);
        return -1;
    }
    
    if(s_time.minute >= 60){
        debug_err("error:Configuration failed bit[%d], minute[%d] to [%d] \r\n", bit, s_time.minute, s_time.minute + minute_add);
        return -1;
    }
    
    s_time.second = 0;
    
    return 0;
}

